{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find hospitals closest to an incident"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `network` module of the ArcGIS API for Python can be used to solve different types of network analysis operations. In this sample, we see how to find the hospital that is closest to an incident."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Find-hospitals-closest-to-an-incident\" data-toc-modified-id=\"Find-hospitals-closest-to-an-incident-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Find hospitals closest to an incident</a></span><ul class=\"toc-item\"><li><span><a href=\"#Closest-facility\" data-toc-modified-id=\"Closest-facility-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Closest facility</a></span><ul class=\"toc-item\"><li><span><a href=\"#Connect-to-your-GIS\" data-toc-modified-id=\"Connect-to-your-GIS-1.1.1\"><span class=\"toc-item-num\">1.1.1&nbsp;&nbsp;</span>Connect to your GIS</a></span></li><li><span><a href=\"#Create-a-Network-Layer\" data-toc-modified-id=\"Create-a-Network-Layer-1.1.2\"><span class=\"toc-item-num\">1.1.2&nbsp;&nbsp;</span>Create a Network Layer</a></span></li><li><span><a href=\"#Create-hospitals-layer\" data-toc-modified-id=\"Create-hospitals-layer-1.1.3\"><span class=\"toc-item-num\">1.1.3&nbsp;&nbsp;</span>Create hospitals layer</a></span></li><li><span><a href=\"#Create-incidents-layer\" data-toc-modified-id=\"Create-incidents-layer-1.1.4\"><span class=\"toc-item-num\">1.1.4&nbsp;&nbsp;</span>Create incidents layer</a></span></li></ul></li><li><span><a href=\"#Solve-for-closest-hospital\" data-toc-modified-id=\"Solve-for-closest-hospital-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Solve for closest hospital</a></span></li><li><span><a href=\"#Analyze-the-results-in-a-table\" data-toc-modified-id=\"Analyze-the-results-in-a-table-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>Analyze the results in a table</a></span><ul class=\"toc-item\"><li><span><a href=\"#Conclusion\" data-toc-modified-id=\"Conclusion-1.3.1\"><span class=\"toc-item-num\">1.3.1&nbsp;&nbsp;</span>Conclusion</a></span></li></ul></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Closest facility"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The closest facility solver provides functionality for finding out the closest locations to a particular input point. This solver would be useful in cases when you have an incident and need to find the closest facility or need to get information on the travel time and the distance to each of the facilities from an incident point for reporting purposes.\n",
    "\n",
    "![](http://desktop.arcgis.com/en/arcmap/latest/extensions/network-analyst/GUID-96C273DB-6A24-4D42-AADA-975A33B44F3D-web.png)\n",
    "\n",
    "When finding closest facilities, you can specify how many to find and whether the direction of travel is toward or away from them. The closest facility solver displays the best routes between incidents and facilities, reports their travel costs, and returns driving directions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Connect to your GIS\n",
    "As a first step, you would need to establish a connection to your organization which could be an ArcGIS Online organization or an ArcGIS Enterprise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import HTML\n",
    "import pandas as pd\n",
    "from arcgis.gis import GIS\n",
    "\n",
    "#connect to your GIS\n",
    "my_gis = GIS(\"home\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a Network Layer\n",
    "To perform any network analysis (such as finding the closest facility, the best route between multiple stops, or service area around a facility), you would need to create a `NetworkLayer` object. In this sample, since we are solving for closest facilities, we need to create a `ClosestFacilityLayer` which is a type of `NetworkLayer`.\n",
    "\n",
    "To create any `NetworkLayer` object, you would need to provide the URL to the appropriate network analysis service. Hence, in this sample, we provide a `ClosestFacility` URL to create a `ClosestFacilityLayer` object. \n",
    "\n",
    "Since all ArcGIS Online organizations already have access to those routing services, you can access this URL through the `GIS` object's `helperServices` property. If you have your own ArcGIS Server based map service with network analysis capability enabled, you would need to provide the URL for this service.\n",
    "\n",
    "Let us start by importing the `network` module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import arcgis.network as network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Access the analysis URL from the `GIS` object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://route.arcgis.com/arcgis/rest/services/World/ClosestFacility/NAServer/ClosestFacility_World'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analysis_url = my_gis.properties.helperServices.closestFacility.url\n",
    "analysis_url"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a `ClosestFacilityLayer` object using this URL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cf_layer = network.ClosestFacilityLayer(analysis_url, gis=my_gis)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create hospitals layer\n",
    "In this sample, we will be looking for the closest hospital (facility) to an incident location. Even though we are interested in finding out the closest one, it would still be helpful to get the information on the distance and travel time to all of them for reference purposes.\n",
    "\n",
    "In the code below, we need to geocode the hospitals' addresses as well as do the reverse geocode for the incident location which has been supplied in the latitude/longitude format.\n",
    "\n",
    "To perform the geocode operations, we import the `geocoding` module of the ArcGIS API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis import geocoding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this sample, we geocode addresses of hospitals to create the facility layer. In your workflows, this could any feature layer. Create a list of hospitals in Rio de Janeiro, Brazil."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "hospitals_addresses = ['Estrada Adhemar Bebiano, 339 Del Castilho, Rio de Janeiro RJ, 21051-370, Brazil',\n",
    "                       'R. José dos Reis Engenho de Dentro, Rio de Janeiro RJ, 20750-000, Brazil',\n",
    "                       'R. Dezessete, s/n Maré, Rio de Janeiro RJ, 21042-010, Brazil',\n",
    "                       'Rua Dr. Miguel Vieira Ferreira, 266 Ramos, Rio de Janeiro RJ, Brazil']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Loop through each address and geocode it. The geocode operation returns a list of matches for each address. We pick the first result and extract the coordinates from it and construct a `Feature` object out of it. Then we combine all the `Feature`s representing the hospitals into a `FeatureSet` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.features import Feature, FeatureSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "hosp_feat_list = []\n",
    "\n",
    "for address in hospitals_addresses:\n",
    "    hit = geocoding.geocode(address)[0]\n",
    "    hosp_feat = Feature(geometry=hit['location'], attributes=hit['attributes'])\n",
    "\n",
    "    hosp_feat_list.append(hosp_feat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Construct a `FeatureSet` using each hospital `Feature`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "hospitals_fset = FeatureSet(features=hosp_feat_list, \n",
    "                            geometry_type='esriGeometryPoint', \n",
    "                            spatial_reference={'wkid' : 4326, 'latestWkid': 4326})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets draw our hospitals on a map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "918c52dd7b2547f59e9ec670025b93d4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"map-static-img-preview-27637e30-0ca3-40ad-b08a-102655abb933\"><img src=\"\"></img></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"map-html-embed-preview-27637e30-0ca3-40ad-b08a-102655abb933\"></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "map1 = my_gis.map('Rio de Janeiro, Brazil')\n",
    "map1.basemap = 'arcgis-light-gray'\n",
    "map1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image](https://user-images.githubusercontent.com/13968196/234745611-6b2b4636-25e1-43cf-a60d-328e8b6a3aae.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "map1.draw(hospitals_fset, symbol={\"type\": \"esriSMS\",\"style\": \"esriSMSSquare\",\n",
    "                                  \"color\": [76,115,0,255],\"size\": 8,})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create incidents layer\n",
    "Similarly, let us create the incient layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "incident_coords = '-43.281206,-22.865676'\n",
    "reverse_geocode = geocoding.reverse_geocode({\"x\": incident_coords.split(',')[0], \n",
    "                                              \"y\": incident_coords.split(',')[1]})\n",
    "\n",
    "incident_feature = Feature(geometry=reverse_geocode['location'], \n",
    "                           attributes=reverse_geocode['address'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "incident_fset = FeatureSet([incident_feature], geometry_type='esriGeometryPoint',\n",
    "                          spatial_reference={'wkid' : 4326, 'latestWkid': 4326})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us add the incident to the map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "map1.draw(incident_fset, symbol={\"type\": \"esriSMS\",\"style\": \"esriSMSCircle\",\"size\": 8})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solve for closest hospital\n",
    "By default the closest facility service would return only the closest location, so we need to specify explicitly the `default_target_facility_count` parameter as well as `return_facilities`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = cf_layer.solve_closest_facility(incidents=incident_fset,\n",
    "                                        facilities=hospitals_fset,\n",
    "                                        default_target_facility_count=4,\n",
    "                                        return_facilities=True,\n",
    "                                        impedance_attribute_name='TravelTime',\n",
    "                                        accumulate_attribute_names=['Kilometers','TravelTime'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us inspect the result dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['requestID', 'routes', 'facilities', 'messages'])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us use the `routes` dictionary to construct line features out of the routes to display on the map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['fieldAliases', 'geometryType', 'spatialReference', 'fields', 'features'])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['routes'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['attributes', 'geometry'])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['routes']['features'][0].keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Construct line features out of the routes that are returned."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "line_feat_list = []\n",
    "for line_dict in result['routes']['features']:\n",
    "    f1 = Feature(line_dict['geometry'], line_dict['attributes'])\n",
    "    line_feat_list.append(f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "routes_fset = FeatureSet(line_feat_list, \n",
    "                         geometry_type=result['routes']['geometryType'],\n",
    "                         spatial_reference= result['routes']['spatialReference'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the routes back to the map. The route to the closest hospital is in red"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "allocation_line_symbol_4 = {'type': 'esriSLS', 'style': 'esriSLSSolid',\n",
    "                                'color': [0,0,255,100], 'width': 6}\n",
    "map1.draw(routes_fset, symbol = allocation_line_symbol_4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image](https://user-images.githubusercontent.com/13968196/234746249-1dddbca5-11b5-44d5-97a9-79aeb47837b9.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "map1.clear_graphics()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analyze the results in a table\n",
    "Since we parsed the routes as a `FeatureSet`, we can display the attributes easily as a `pandas` `DataFrame`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ObjectID</th>\n",
       "      <th>FacilityID</th>\n",
       "      <th>FacilityRank</th>\n",
       "      <th>Name</th>\n",
       "      <th>IncidentCurbApproach</th>\n",
       "      <th>FacilityCurbApproach</th>\n",
       "      <th>IncidentID</th>\n",
       "      <th>Total_TravelTime</th>\n",
       "      <th>Total_Miles</th>\n",
       "      <th>Total_Kilometers</th>\n",
       "      <th>Shape_Length</th>\n",
       "      <th>SHAPE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Rua Tangapeme 133 - Location 1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4.713672</td>\n",
       "      <td>1.363773</td>\n",
       "      <td>2.194747</td>\n",
       "      <td>0.020689</td>\n",
       "      <td>{\"paths\": [[[-43.28127999999998, -22.865709999...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>Rua Tangapeme 133 - Location 4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>11.12642</td>\n",
       "      <td>3.048409</td>\n",
       "      <td>4.905807</td>\n",
       "      <td>0.045753</td>\n",
       "      <td>{\"paths\": [[[-43.28127999999998, -22.865709999...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>Rua Tangapeme 133 - Location 3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12.780526</td>\n",
       "      <td>4.026303</td>\n",
       "      <td>6.479476</td>\n",
       "      <td>0.061724</td>\n",
       "      <td>{\"paths\": [[[-43.28127999999998, -22.865709999...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>Rua Tangapeme 133 - Location 2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>16.400918</td>\n",
       "      <td>4.829731</td>\n",
       "      <td>7.77262</td>\n",
       "      <td>0.073508</td>\n",
       "      <td>{\"paths\": [[[-43.28127999999998, -22.865709999...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ObjectID  FacilityID  FacilityRank                            Name  \\\n",
       "0         1           1             1  Rua Tangapeme 133 - Location 1   \n",
       "1         2           4             2  Rua Tangapeme 133 - Location 4   \n",
       "2         3           3             3  Rua Tangapeme 133 - Location 3   \n",
       "3         4           2             4  Rua Tangapeme 133 - Location 2   \n",
       "\n",
       "   IncidentCurbApproach  FacilityCurbApproach  IncidentID  Total_TravelTime  \\\n",
       "0                     1                     1           1          4.713672   \n",
       "1                     1                     2           1          11.12642   \n",
       "2                     1                     1           1         12.780526   \n",
       "3                     1                     1           1         16.400918   \n",
       "\n",
       "   Total_Miles  Total_Kilometers  Shape_Length  \\\n",
       "0     1.363773          2.194747      0.020689   \n",
       "1     3.048409          4.905807      0.045753   \n",
       "2     4.026303          6.479476      0.061724   \n",
       "3     4.829731           7.77262      0.073508   \n",
       "\n",
       "                                               SHAPE  \n",
       "0  {\"paths\": [[[-43.28127999999998, -22.865709999...  \n",
       "1  {\"paths\": [[[-43.28127999999998, -22.865709999...  \n",
       "2  {\"paths\": [[[-43.28127999999998, -22.865709999...  \n",
       "3  {\"paths\": [[[-43.28127999999998, -22.865709999...  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = routes_fset.sdf\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us add the hospital addresses and incident address to this table and display only the relevant columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1['facility_address'] = hospitals_addresses\n",
    "df1['incident_address'] = [incident_feature.attributes['Match_addr'] for i in range(len(hospitals_addresses))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>facility_address</th>\n",
       "      <th>incident_address</th>\n",
       "      <th>Total_Miles</th>\n",
       "      <th>Total_TravelTime</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Estrada Adhemar Bebiano, 339 Del Castilho, Rio...</td>\n",
       "      <td>Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...</td>\n",
       "      <td>1.363773</td>\n",
       "      <td>4.713672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>R. José dos Reis Engenho de Dentro, Rio de Jan...</td>\n",
       "      <td>Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...</td>\n",
       "      <td>3.048409</td>\n",
       "      <td>11.12642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>R. Dezessete, s/n Maré, Rio de Janeiro RJ, 210...</td>\n",
       "      <td>Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...</td>\n",
       "      <td>4.026303</td>\n",
       "      <td>12.780526</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Rua Dr. Miguel Vieira Ferreira, 266 Ramos, Rio...</td>\n",
       "      <td>Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...</td>\n",
       "      <td>4.829731</td>\n",
       "      <td>16.400918</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                    facility_address  \\\n",
       "0  Estrada Adhemar Bebiano, 339 Del Castilho, Rio...   \n",
       "1  R. José dos Reis Engenho de Dentro, Rio de Jan...   \n",
       "2  R. Dezessete, s/n Maré, Rio de Janeiro RJ, 210...   \n",
       "3  Rua Dr. Miguel Vieira Ferreira, 266 Ramos, Rio...   \n",
       "\n",
       "                                    incident_address  Total_Miles  \\\n",
       "0  Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...     1.363773   \n",
       "1  Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...     3.048409   \n",
       "2  Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...     4.026303   \n",
       "3  Rua Tangapeme 133, Inhaúma, Rio de Janeiro, 20...     4.829731   \n",
       "\n",
       "   Total_TravelTime  \n",
       "0          4.713672  \n",
       "1          11.12642  \n",
       "2         12.780526  \n",
       "3         16.400918  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1[['facility_address','incident_address','Total_Miles','Total_TravelTime']]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Conclusion\n",
    "Thus using the `network` module of the ArcGIS API for Python, you can solve for closest facilities from an incident location."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "9.0"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
